package com.baizhi.dao;



import com.baizhi.entity.Title;
import com.baizhi.util.JDBCUtil;
import com.baizhi.util.LowJdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class Dao {
    public List<Title>querryslurall(String str){
        Connection conn=null;
        PreparedStatement pstmt=null;
        ResultSet rs=null;
        String sql;
        conn= LowJdbcUtil.getConn();
        try{
            sql="SELECT *  FROM title where title like? or keyWord like? or text like?";
            pstmt=conn.prepareStatement(sql);
            pstmt.setString(1,"%"+str+"%");
            pstmt.setString(2,"%"+str+"%");
            pstmt.setString(3,"%"+str+"%");
            rs=pstmt.executeQuery();
            List<Title>list=new ArrayList<>();
            Title a=null;
            while(rs.next()){
                a=new Title();
                a.setId(rs.getInt(1));
                a.setTitle(rs.getString(2));
                a.setText(rs.getString(3));
                a.setDate(rs.getString(4));
                a.setKeyWord(rs.getString(5));
                a.setLikeCount(rs.getInt(6));
                a.setName(rs.getString(7));
                list.add(a);
            }
            return  list;
        }catch(SQLException e){
            e.printStackTrace();
            return null;
        }finally{
            try{
                LowJdbcUtil.closeConn();
            }catch(Exception e){
                e.printStackTrace();
            }

        }
    }
    public List<Title> queryallmy (String str,Title title){
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql;
        conn = LowJdbcUtil.getConn();
        try{
            sql="SELECT * FROM title where title like? or keyWord like? or text like? and name=?";
            pstmt=conn.prepareStatement(sql);
            pstmt.setString(1,"%"+str+"%");
            pstmt.setString(2,"%"+str+"%");
            pstmt.setString(3,"%"+str+"%");
            pstmt.setString(4,title.getName());
            rs=pstmt.executeQuery();
            List<Title>list=new ArrayList<>();
            Title a = null;
            while(rs.next()){
                a=new Title();
                a.setId(rs.getInt(1));
                a.setTitle(rs.getString(2));
                a.setText(rs.getString(3));
                a.setDate(rs.getString(4));
                a.setKeyWord(rs.getString(5));
                a.setLikeCount(rs.getInt(6));
                a.setName(rs.getString(7));
                list.add(a);
            }
            return  list;
        }catch(SQLException e){
            e.printStackTrace();
            return null;
        }finally{
            try{
                LowJdbcUtil.closeConn();
            }catch(Exception e){
                e.printStackTrace();
            }

        }
    }
}
